


var EXPORTED_SYMBOLS = ["Clipboard"];

let Clipboard = (function() {
    "use strict";

    const Cu = Components.utils;
    const Ci = Components.interfaces;
    const Cc = Components.classes;
    function require(url) {
        let scope = {};
        Cu.import(url, scope);
        return scope;
    }

    let {Services} = require("resource://gre/modules/Services.jsm");

    return {
        putString(txt) {
            let str = Cc["@mozilla.org/supports-string;1"].
                createInstance(Ci.nsISupportsString);
            str.data = txt;
            let trans = Cc["@mozilla.org/widget/transferable;1"].
                createInstance(Ci.nsITransferable);
            trans.addDataFlavor("text/unicode");
            trans.setTransferData("text/unicode", str, txt.length * 2);
            Services.clipboard.setData(
                trans, null, Ci.nsIClipboard.kGlobalClipboard
            );
        },

        getString() {
            let has_data = Services.clipboard
                .hasDataMatchingFlavors(
                    ["text/unicode"], 1,
                    Ci.nsIClipboard.kGlobalClipboard
                );
            if (!has_data)
                return null;
            let trans = Cc["@mozilla.org/widget/transferable;1"].
                createInstance(Ci.nsITransferable);
            trans.addDataFlavor("text/unicode");
            Services.clipboard.getData(trans, Ci.nsIClipboard.kGlobalClipboard);
            let str = {}, len = {};
            trans.getTransferData("text/unicode", str, len);
            str = str.value.QueryInterface(Ci.nsISupportsString);  
            return str.data.substring(0, len.value/2);  
        }
    };
})();

